package com.tencreat.mall.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.zxing.WriterException;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.exception.CustomException;
import com.tencreat.common.utils.QRCodeGenerator;
import com.tencreat.domain.account.MallStore;
import com.tencreat.domain.account.MallUser;
import com.tencreat.domain.mall.MallOrder;
import com.tencreat.domain.mall.MallStoreSetmeal;
import com.tencreat.domain.mall.dto.MallGoodsSearchDTO;
import com.tencreat.domain.mall.vo.MallGoodsListVO;
import com.tencreat.service.account.IMallStoreService;
import com.tencreat.service.account.IMallUserService;
import com.tencreat.service.mall.IMallGoodsService;
import com.tencreat.service.mall.IMallStoreSetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.util.List;

/**
 * 描述信息
 *
 * @Author yaoruiqi
 * @Company: 深圳腾创互联科技有限公司
 * @link: https://tencreat.com
 * @Date: 2023/11/7 17:00
 */

@Api(tags = "66-店铺相关api")
@RestController
@RequestMapping("/api/mallStore")
public class MallStoreApiController extends BaseController {
    @Autowired
    private IMallStoreService mallStoreService;//店铺

    @Autowired
    private IMallUserService mallUserService;
    @Autowired
    private IMallStoreSetmealService mallStoreSetmealService;

    @Autowired
    private IMallGoodsService mallGoodsService;

    @ApiOperation("店铺收款二维码")
    @PostMapping(value = "/qrimage")
    public HttpResult<ResponseEntity<byte[]>> getQRImage() {
        Long uuid = getUuid();
        MallStore mallStore = mallStoreService.lambdaQuery()
                .eq(MallStore::getUuid, uuid)
                .eq(MallStore::getApprovalStatus, 1)
                .one();
        if (mallStore == null) {
            throw new CustomException("请先成为店铺");
        }
        MallUser user = mallUserService.lambdaQuery().eq(MallUser::getUuid, uuid).one();
        //二维码内的信息
        String[] insf = {"craftsman", String.valueOf(mallStore.getId()), String.valueOf(uuid), mallStore.getPhoto(), user.getMobile()};
        String info = String.join(",", insf);
        byte[] qrcode = null;
        try {
            qrcode = QRCodeGenerator.getQRCodeImage(info, 360, 360);
        } catch (WriterException e) {
            System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("Could not generate QR Code, IOException :: " + e.getMessage());
        }

        // Set headers
        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);
        return HttpResult.ok(new ResponseEntity<byte[]>(qrcode, headers, HttpStatus.CREATED));
    }

    @ApiOperation(value = "查询店铺")
    @PostMapping("/storeQualification")
    HttpResult<MallStore> storeQualification(@RequestBody ReqIdParam idParam) {
        MallStore mallStore = mallStoreService.lambdaQuery().eq(MallStore::getId, idParam.getId()).one();
        return HttpResult.ok(mallStore);
    }


    @ApiOperation(value = "查询套餐分类列表")
    @PostMapping("/mallStoreSetmealList")
    public HttpResult<IPage<MallStoreSetmeal>> mallStoreSetmealList(@RequestBody MallStoreSetmeal mallStoreSetmeal) {

        IPage<MallStoreSetmeal> page = new Page<>(mallStoreSetmeal.getPageNum(), mallStoreSetmeal.getPageSize());

        MallStore mallStore = mallStoreService.lambdaQuery().eq(MallStore::getId, mallStoreSetmeal.getStoreId()).one();
        if (mallStore != null) {
            mallStoreSetmeal.setUserId(mallStore.getUserId());
        }
        return HttpResult.ok(mallStoreSetmealService.page(page, mallStoreSetmeal));
//        return HttpResult.ok(mallStoreSetmealService.selectList(wrapper));
    }


    @ApiOperation(value = "商品列表  推荐商品")
    @PostMapping("/recommendList")
    public HttpResult<IPage<MallGoodsListVO>> recommendList(@RequestBody MallGoodsSearchDTO mallGoodsSearchPo) {

        return HttpResult.ok(mallGoodsService.recommendList(mallGoodsSearchPo));
    }
}
